/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import DAL.TourBookingBean;
import java.io.*;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

/**
 *
 * @author Mr-aUgust
 */
@WebServlet(name = "ControllerCancelTour", urlPatterns = {"/ControllerCancelTour"})
public class ControllerCancelTour extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
         HttpSession session=request.getSession(true);
        TourBookingBean tb=new TourBookingBean();
        
//        String Bookingid=request.getParameter("Bookingid");
//   
      String usname=(String) session.getAttribute("User");
//        
//        String totalmoney=request.getParameter("totalmoney");        
//        float totalMoney=Float.parseFloat(totalmoney);       
      
        String Bookingid=session.getAttribute("tourBookingId").toString();
        int id=Integer.parseInt(Bookingid);
        String total=session.getAttribute("crefundMoney").toString();
        float refunMoney=Float.parseFloat(total);
        String tourCan="True";        
        Date date=new Date();
        SimpleDateFormat simpleDate=new SimpleDateFormat("MM/dd/yyyy");        
        String nowdate=simpleDate.format(date);      
      
//       
//       int numOfday=tb.getDayOfTour(usname,Bookingid);        
//      
//        double refunMoney=0;
//        
//        if(numOfday<=0)
//        {
//          out.println("<script>alert('You can't cancel this tour. Because this tour is using !');window.location='index.jsp?page=listTour';</script>");
// 
//        }
//        else if(numOfday==1) {
//            refunMoney=totalMoney*0.75;
//        }
//        else if(numOfday==2) {
//            refunMoney=totalMoney*0.8;
//        }
//        else if(numOfday==3) {
//            refunMoney=totalMoney*0.85;
//        }
//        else if(numOfday==4) {
//            refunMoney=totalMoney*0.9;
//        }
//        else {
//            refunMoney=totalMoney*0.95;
//        }
      
        boolean kq=tb.CancelTourBooking(usname,id,tourCan,nowdate,refunMoney);
        if(kq==true)
        {
         out.println("<script>alert('You cancel success tour !');window.location='index.jsp?page=BillBookTours';</script>");
          
        }
        else
        {
           out.println("<script>alert('You cancel success tour !');window.location='index.jsp?page=MyCarBook';</script>");
 
        }        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
